package cn.dida.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.dida.dao.QrcodeAutoProductDao;
import cn.dida.dao.QrcodeDao;
import cn.dida.model.Pageable;
import cn.dida.model.UserSession;
import cn.dida.service.QrcodeAutoProductService;
import cn.dida.util.DateUtil;
import cn.dida.util.Interval;
import cn.dida.util.TimeSlotUtil2;


/**
 * 自动绑定实现接口类
 * @author kelan
 * @date 2018年1月19日
 */
@Service
public class QrcodeAutoProductServiceImpl implements QrcodeAutoProductService {

	@Autowired
	private QrcodeAutoProductDao qrcodeAutoProductDao;
	@Autowired
	private QrcodeDao qrcodeDao;
	
	@Override
	public Pageable<Map<String, Object>> queryLgsQrcodeExtendList(Integer companyId, Map<String, Object> paramMap,
			Integer page) {
		// TODO Auto-generated method stub
		return qrcodeAutoProductDao.queryLgsQrcodeExtendList(companyId, paramMap, page);
	}

	@Override
	public Pageable<Map<String, Object>> queryLgsQrcodeExtendDetail(Integer companyId,String prodBillcode, Map<String, Object> paramMap,
			Integer page) {
		// TODO Auto-generated method stub
		return qrcodeAutoProductDao.queryLgsQrcodeExtendDetail(companyId,prodBillcode, paramMap, page);
	}

	@Override
	public List<Map<String, Object>> queryLgsQrcodeExtendListNopage(Integer companyId, Map<String, Object> paramMap) {
		// TODO Auto-generated method stub
		return qrcodeAutoProductDao.queryLgsQrcodeExtendListNopage(companyId,paramMap);
	}

	@Override
	public List<Map<String, Object>> queryLgsQrcodeExtendDetailNopage(Integer companyId, Map<String, Object> paramMap) {
		// TODO Auto-generated method stub
		return qrcodeAutoProductDao.queryLgsQrcodeExtendDetailNopage(companyId,paramMap);
	}

	@Override
	public Integer updateQrcodeProductActSta(String prod_date, String prodBillcode , UserSession userSession, Integer type) {
		// TODO Auto-generated method stub
		//说明 没有生成pro_product_qrcode 记录，需要生成二维码绑定记录
//		if(id == null){
		//获取改单据号 PIN码内所有瓶码的最大号和最小号 
		Long produce_date = NumberUtils.toLong(String.valueOf(DateUtil.strToDate(prod_date,"yyyyMMdd HH:mm:ss").getTime()), 0L);
		List<Map<String, Object>>  list = qrcodeAutoProductDao.selectMinIDAndMaxIDBylgsQrcodeExtendQrcodeId(prodBillcode,userSession.getCompanyId());
		for(Map<String, Object> map : list){
			
			Integer pid = Integer.parseInt(map.get("product_id").toString());
			Long qcid = Long.parseLong(map.get("qrcode_baseid").toString());
			Long pms = Long.parseLong(map.get("minId").toString());
			Long pme = Long.parseLong(map.get("maxId").toString());
			//定义最大值和最小值区间
			Interval interval = new Interval(pms,pme);
			String batch_nub = map.get("prod_batchno").toString();
			//查询该区间瓶码是否已经生成了二维码绑定记录
			List<Map<String, Object>>  bandlist = qrcodeAutoProductDao.selectBandingOfQrcodeList(qcid, pid);
			if(bandlist.size() <= 0){
				// 插入绑定关系
				Integer ppqid = qrcodeDao.insertQrcodeProduct(pid, qcid, 1, pms, pme, userSession,batch_nub,produce_date,1);
				// 更新绑定详情 pro_product_qrcode_id
				qrcodeAutoProductDao.updateQrcodeDetailOfProduct(qcid, ppqid, pid, prodBillcode, userSession.getCompanyId());
				//更新自动绑定启用营销状态 为启用和二维码生成记录pro_product_qrcode_id
				qrcodeAutoProductDao.updateLgsQrcodeExtendByisact(qcid, ppqid, pid, prodBillcode, userSession.getCompanyId(),1);
				//更新营销启用状态
				qrcodeDao.updateQrcodeProductActSta(ppqid,1,userSession);
			}else{
				Integer ppqid = null;
				//返回最大值和最小值 新的区间值
				TimeSlotUtil2 demo = new TimeSlotUtil2();
				Interval retinterval = null;
				//取交集，比较区间 取最小值，取最大值
				Interval[] tempInterval = new Interval[bandlist.size()];
				int k = 0;
				for(Map<String, Object> bandmap : bandlist){
					Long start = Long.valueOf(bandmap.get("qrcode_s").toString());
					Long end = Long.valueOf(bandmap.get("qrcode_e").toString());
					ppqid = Integer.valueOf(bandmap.get("id").toString());
					tempInterval[k] = new Interval(start,end);
					//返回最大值和最小值 新的区间值
			        Interval val = demo.getOverlappingCount(tempInterval[k],interval);
			        if(val != null){
			        	retinterval = val;
			        	break;
			        }
					k++;
				}
				
		        //retinterval == null 代表无交集
		        if(retinterval == null ){
					// 插入绑定关系
					ppqid = qrcodeDao.insertQrcodeProduct(pid, qcid, 1, pms, pme, userSession,batch_nub,produce_date,1);
					// 更新绑定详情 pro_product_qrcode_id
					qrcodeAutoProductDao.updateQrcodeDetailOfProduct(qcid, ppqid, pid, prodBillcode, userSession.getCompanyId());
					//更新自动绑定启用营销状态 为启用和二维码生成记录pro_product_qrcode_id
					qrcodeAutoProductDao.updateLgsQrcodeExtendByisact(qcid, ppqid, pid, prodBillcode, userSession.getCompanyId(),1);
					//更新营销启用状态
					qrcodeDao.updateQrcodeProductActSta(ppqid,1,userSession);
		        }else{
		        	//更新 绑定关系的最大值和最小值
		        	qrcodeAutoProductDao.updateProProductQrcodeByqrcode_s_e(ppqid, retinterval.start, retinterval.end);
		        	// 更新绑定详情 pro_product_qrcode_id
					qrcodeAutoProductDao.updateQrcodeDetailOfProduct(qcid, ppqid, pid, prodBillcode, userSession.getCompanyId());
					//更新自动绑定启用营销状态 为启用和二维码生成记录pro_product_qrcode_id
					qrcodeAutoProductDao.updateLgsQrcodeExtendByisact(qcid, ppqid, pid, prodBillcode, userSession.getCompanyId(),1);
					//更新营销启用状态
					qrcodeDao.updateQrcodeProductActSta(ppqid,1,userSession);
		        }
			}
			
		}
		
		//获取改单据号 内所有箱码的最大号和最小号 
		List<Map<String, Object>>  boxlist = qrcodeAutoProductDao.selectMinIDAndMaxIDBylgsQrcodeExtendOuterboxeId(prodBillcode,userSession.getCompanyId());
		for(Map<String, Object> map : boxlist){
			Integer pid = Integer.parseInt(map.get("product_id").toString());
			Long qcid = Long.parseLong(map.get("qrcode_baseid").toString());
			Long pms = Long.parseLong(map.get("minId").toString());
			Long pme = Long.parseLong(map.get("maxId").toString());
			//定义最大值和最小值区间
			Interval interval = new Interval(pms,pme);
			String batch_nub = map.get("prod_batchno").toString();
			//查询该区间瓶码是否已经生成了二维码绑定记录
			List<Map<String, Object>>  bandlist = qrcodeAutoProductDao.selectBandingOfQrcodeList(qcid, pid);
			if(bandlist.size() <= 0){
				// 插入绑定关系
				Integer ppqid = qrcodeDao.insertQrcodeProduct(pid, qcid, 2, pms, pme, userSession,batch_nub,produce_date,1);
				// 更新绑定详情 pro_product_qrcode_id
				qrcodeAutoProductDao.updateQrcodeBoxDetailOfProduct(qcid, ppqid, pid, prodBillcode, userSession.getCompanyId());
				//更新营销启用状态
				qrcodeDao.updateQrcodeProductActSta(ppqid,0,userSession);
			}else{
				Integer ppqid = null;
				//返回最大值和最小值 新的区间值
				TimeSlotUtil2 demo = new TimeSlotUtil2();
				Interval retinterval = null;
				//取交集，比较区间 取最小值，取最大值
				Interval[] tempInterval = new Interval[bandlist.size()];
				int k = 0;
				for(Map<String, Object> bandmap : bandlist){
					Long start = Long.valueOf(bandmap.get("qrcode_s").toString());
					Long end = Long.valueOf(bandmap.get("qrcode_e").toString());
					ppqid = Integer.valueOf(bandmap.get("id").toString());
					tempInterval[k] = new Interval(start,end);
					//返回最大值和最小值 新的区间值
			        Interval val = demo.getOverlappingCount(tempInterval[k],interval);
			        if(val != null){
			        	retinterval = val;
			        	break;
			        }
					k++;
				}
		        //retinterval == null 代表无交集
		        if(retinterval == null ){
		        	// 插入绑定关系
					ppqid = qrcodeDao.insertQrcodeProduct(pid, qcid, 2, pms, pme, userSession,batch_nub,produce_date,1);
					// 更新绑定详情 pro_product_qrcode_id
					qrcodeAutoProductDao.updateQrcodeBoxDetailOfProduct(qcid, ppqid, pid, prodBillcode, userSession.getCompanyId());
					//更新营销启用状态
					qrcodeDao.updateQrcodeProductActSta(ppqid,0,userSession);
		        }else{
		        	//更新 绑定关系的最大值和最小值
		        	qrcodeAutoProductDao.updateProProductQrcodeByqrcode_s_e(ppqid, retinterval.start, retinterval.end);
		        	// 更新绑定详情 pro_product_qrcode_id
					qrcodeAutoProductDao.updateQrcodeDetailOfProduct(qcid, ppqid, pid, prodBillcode, userSession.getCompanyId());
					//更新营销启用状态
					qrcodeDao.updateQrcodeProductActSta(ppqid,0,userSession);
					
		        }
				
			}
			
		}
		
		return null;
	}
	
}
